/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.webapp.messages;

import cz.muni.fi.diplomka.servicelayer.dto.Message;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.MessageManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.PlayerManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.MessageManagerImpl;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.PlayerManagerImpl;
import cz.muni.fi.diplomka.webapp.layout.LinkPanel;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;

/**
 *
 * @author Jakub Kutil
 */
public class MessageSentPage extends WebPage {

    private MessageManager messageManager = new MessageManagerImpl();
    private PlayerManager playerManager = new PlayerManagerImpl();
    private Long id;
    
    public MessageSentPage(PageParameters parameters) {

        add(new FeedbackPanel("feedback"));

        if (parameters.containsKey("id")) {
            id = Long.parseLong(parameters.getString("id"));

            PageParameters p = new PageParameters();
            p.add("id", id.toString());

            add(new LinkPanel("linkPanel", p));
            add(new MessagePanel("messagePanel", p));

            List<Message> list = new ArrayList<>();


            for (Message m : messageManager.getAllSentMessagesOfPlayer(id)) {
                list.add(m);
            }

            ListView listview = new ListView("listview", list) {
                @Override
                protected void populateItem(ListItem item) {
                    Message message = (Message) item.getModelObject();
                    item.add(new Label("to", playerManager.findPlayerById(message.getIdTo()).getNickName()));
                    item.add(new Label("timestamp", message.getTime().toString()));
                    PageParameters pageParameters = new PageParameters();
                    pageParameters.add("id", id.toString());
                    pageParameters.add("messageId", message.getId().toString());
                    pageParameters.add("type", "sent");
                    BookmarkablePageLink<Void> link = new BookmarkablePageLink<>("link", MessagePage.class, pageParameters);
                    link.add(new Label("subject", message.getSubject()));
                    item.add(link);
                }
            };
            add(listview);

        }
    }
}
